home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / etc / cron.daily / apt < prev    next >
Text File  |  2008-08-14  |  8KB  |  265 lines

  1. #!/bin/sh
  2. #
  3.  
  4. #set -e
  5. #
  6. # This file understands the following apt configuration variables:
  7. #
  8. #  "APT::Periodic::Update-Package-Lists=1"
  9. #  - Do "apt-get update" automatically every n-days (0=disable)
  10. #    
  11. #  "APT::Periodic::Download-Upgradeable-Packages=0",
  12. #  - Do "apt-get upgrade --download-only" every n-days (0=disable)
  13. #  "APT::Periodic::AutocleanInterval"
  14. #  - Do "apt-get autoclean" every n-days (0=disable)
  15. #
  16. #  "APT::Periodic::Unattended-Upgrade"
  17. #  - Run the "unattended-upgrade" security upgrade script 
  18. #    every n-days (0=disabled)
  19. #    Requires the package "unattended-upgrades" and will write
  20. #    a log in /var/log/unattended-upgrades
  21. #  "APT::Archives::MaxAge",
  22. #  - Set maximum allowed age of a cache package file. If a cache 
  23. #    package file is older it is deleted (0=disable)
  24. #
  25. #  "APT::Archives::MaxSize",
  26. #  - Set maximum size of the cache in MB (0=disable). If the cache
  27. #    is bigger, cached package files are deleted until the size
  28. #    requirement is met (the biggest packages will be deleted 
  29. #    first).
  30. #
  31. #  "APT::Archives::MinAge"
  32. #  - Set minimum age of a package file. If a file is younger it
  33. #    will not be deleted (0=disable). Usefull to prevent races 
  34. #    and to keep backups of the packages for emergency.
  35.  
  36. check_stamp()
  37. {
  38.     stamp="$1"
  39.     interval="$2"
  40.  
  41.     if [ $interval -eq 0 ]; then
  42.         return 1
  43.     fi
  44.  
  45.     if [ ! -f $stamp ]; then
  46.         return 0
  47.     fi
  48.  
  49.     # compare midnight today to midnight the day the stamp was updated
  50.     stamp=$(date --date=$(date -r $stamp --iso-8601) +%s)
  51.     now=$(date --date=$(date --iso-8601) +%s)
  52.     delta=$(($now-$stamp))
  53.  
  54.     # intervall is in days,
  55.     interval=$(($interval*60*60*24))
  56.     #echo "stampfile: $1"
  57.     #echo "interval=$interval, now=$now, stamp=$stamp, delta=$delta"
  58.  
  59.     if [ $delta -ge $interval ]; then
  60.         return 0
  61.     fi
  62.  
  63.     return 1
  64. }
  65.  
  66. update_stamp()
  67. {
  68.     stamp="$1"
  69.  
  70.     touch $stamp
  71. }
  72.  
  73.  
  74.  
  75. # we check here if autoclean was enough sizewise
  76. check_size_constraints()
  77. {
  78.     # min-age in days
  79.     MaxAge=0
  80.     MinAge=2
  81.     MaxSize=0
  82.     CacheDir="var/cache/apt"
  83.     CacheArchive="archives/"
  84.     eval $(apt-config shell MaxAge APT::Archives::MaxAge)
  85.     eval $(apt-config shell MinAge APT::Archives::MinAge)
  86.     eval $(apt-config shell MaxSize APT::Archives::MaxSize)
  87.     eval $(apt-config shell Dir Dir)
  88.     eval $(apt-config shell CacheDir Dir::Cache)
  89.     eval $(apt-config shell CacheArchive Dir::Cache::archives)
  90.  
  91.     # sanity check
  92.     if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
  93.     echo "empty Dir::Cache or Dir::Cache::archives, exiting"
  94.     exit
  95.     fi
  96.     
  97.     Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
  98.  
  99.     # check age
  100.     if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
  101.     find $Cache -name "*.deb"  \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
  102.     elif [ ! $MaxAge -eq 0 ]; then
  103.     find $Cache -name "*.deb"  -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
  104.     fi
  105.     
  106.     # check size
  107.     if [ ! $MaxSize -eq 0 ]; then
  108.     # maxSize is in MB
  109.     MaxSize=$(($MaxSize*1024))
  110.  
  111.     #get current time
  112.     now=$(date --date=$(date --iso-8601) +%s)
  113.     MinAge=$(($MinAge*24*60*60))
  114.  
  115.     # reverse-sort by mtime
  116.     for file in $(ls -rt $Cache/*.deb 2>/dev/null); do 
  117.         du=$(du -s $Cache)
  118.         size=${du%%/*}
  119.         # check if the cache is small enough
  120.         if [ $size -lt $MaxSize ]; then
  121.         break
  122.         fi
  123.  
  124.         # check for MinAge of the file
  125.         if [ ! $MinAge -eq 0 ]; then 
  126.         # check both ctime and mtime 
  127.         mtime=$(stat -c %Y $file)
  128.         ctime=$(stat -c %Z $file)
  129.         if [ $mtime -gt $ctime ]; then
  130.             delta=$(($now-$mtime))
  131.         else
  132.             delta=$(($now-$ctime))
  133.         fi
  134.         #echo "$file ($delta), $MinAge"
  135.         if [ $delta -le $MinAge ]; then
  136.             #echo "Skiping $file (delta=$delta)"
  137.             break
  138.         fi
  139.         fi
  140.  
  141.         # delete oldest file
  142.         rm -f $file
  143.     done
  144.     fi
  145. }
  146.  
  147. # sleep for a random intervall of time (default 30min)
  148. # (some code taken from cron-apt, thanks)
  149. random_sleep()
  150. {
  151.     RandomSleep=1800
  152.     eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
  153.     if [ $RandomSleep -eq 0 ]; then
  154.     return
  155.     fi
  156.     if [ -z "$RANDOM" ] ; then
  157.         # A fix for shells that do not have this bash feature.
  158.     RANDOM=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -c"1-5")
  159.     fi
  160.     TIME=$(($RANDOM % $RandomSleep))
  161.     sleep $TIME
  162. }
  163.  
  164. # main
  165.  
  166. if ! which apt-config >/dev/null; then
  167.     exit 0
  168. fi
  169.  
  170. UpdateInterval=0
  171. DownloadUpgradeableInterval=0
  172. eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
  173. AutocleanInterval=$DownloadUpgradeableInterval
  174. eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
  175.  
  176. UnattendedUpgradeInterval=0
  177. eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
  178.  
  179.  
  180. # laptop check, on_ac_power returns:
  181. #       0 (true)    System is on mains power
  182. #       1 (false)   System is not on mains power
  183. #       255 (false) Power status could not be determined
  184. # Desktop systems always return 255 it seems
  185. if which on_ac_power >/dev/null; then
  186.     on_ac_power
  187.     if [ $? -eq 1 ]; then
  188.     exit 0
  189.     fi
  190. fi
  191.  
  192. # check if we can lock the cache and if the cache is clean
  193. # There's a reasonable chance that someone is already running an apt
  194. # frontend that has locked the cache, so exit quietly if it is locked.
  195. if ! apt-get check -q -q 2>/dev/null; then
  196.     exit 0
  197. fi
  198.  
  199. # sleep random amount of time
  200. random_sleep
  201.  
  202. # check again if we can access the cache
  203. if ! apt-get check -q -q 2>/dev/null; then
  204.     exit 1
  205. fi
  206.  
  207. # set the proxy based on the admin users gconf settings
  208. admin_user=$(getent group admin|cut -d: -f4|cut -d, -f1)
  209. if [ -n "$admin_user" ] && [ -x /usr/bin/sudo ] && [ -z "$http_proxy" ] && [ -x /usr/bin/gconftool ]; then
  210.     use=$(sudo -u "$admin_user" gconftool --get /system/http_proxy/use_http_proxy)
  211.     host=$(sudo -u "$admin_user" gconftool --get /system/http_proxy/host)
  212.     port=$(sudo -u "$admin_user" gconftool --get /system/http_proxy/port)
  213.     if [ "$use" = "true" ] && [ -n "$host" ] && [ -n "$port" ]; then
  214.         export http_proxy="http://$host:$port/"
  215.     fi
  216. fi
  217.  
  218. # sleep random amount of time
  219. random_sleep
  220.  
  221. # check again if we can access the cache
  222. if ! apt-get check -q -q 2>/dev/null; then
  223.     exit 1
  224. fi
  225.  
  226. UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
  227. if check_stamp $UPDATE_STAMP $UpdateInterval; then
  228.     # check for a new archive signing key (against the master keyring)
  229.     apt-key net-update
  230.     # now run the update
  231.     if apt-get -qq update -o APT::Update::Auth-Failure::="cp /usr/share/apt/apt-auth-failure.note /var/lib/update-notifier/user.d/" 2>/dev/null; then 
  232.     # Could possible test access to '/var/run/dbus/system_bus_socket' has well,
  233.     # but I'm not sure how stable the internal pipe location is defined as
  234.     # being;  so for the moment just 2>/dev/null . --sladen 2007-09-27
  235.     if which dbus-send >/dev/null; then
  236.         dbus-send --system / app.apt.dbus.updated boolean:true 2>/dev/null || true
  237.     fi
  238.         update_stamp $UPDATE_STAMP
  239.     fi
  240. fi
  241.  
  242. DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
  243. if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
  244.     apt-get -qq -d dist-upgrade 2>/dev/null
  245.     update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
  246. fi
  247.  
  248. UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
  249. if check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
  250.     unattended-upgrade
  251.     update_stamp $UPGRADE_STAMP
  252. fi
  253.  
  254. AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
  255. if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
  256.     apt-get -qq autoclean
  257.     update_stamp $AUTOCLEAN_STAMP
  258. fi
  259.  
  260. # check cache size 
  261. check_size_constraints
  262.